Baklava

What is a playfield?

Baklava's sprites (graphical objects) can only appear inside a Baklava "playfield." To provide an environment for your sprites, you must create a baklava.Playfield object and add it to your Java applet. Then you can create as many sprite objects as you like, within the playfield.

Other responsibilities of playfields

In addition to providing an environment for sprites, playfields are also responsible for delivering global timer events to your applet, if you choose to implement the GlobalTimerObsever interface.

Also, playfields have convenience methods to perform tasks such as fetching images from the web server.

Example: An Applet with a Playfield

A very simple applet containing a playfield might look like this. Note that this applet does not use a layout manager and gives the entire applet area to the playfield. It is also possible to create playfields that share the applet with other controls by using a layout manager such as java.awt.GridBagLayout. This playfield contains only one sprite for simplicity.
// Be sure to import the baklava package
import baklava.*;

// A typical applet class declaration
public class MyApplet extends Applet {
	// A reference to the playfield
	Playfield p;

	// The URL from which we will fetch images
	// CHANGE THIS, of course!
	String base = "http://www.mysite.com/graphics/";

	// The web browser calls init when it is ready for the
	// applet to start operating	
	public void init() {
		// Use no layout manager
		setLayout(null);

		// Pass a reference to the applet, as well as
		// the width and height of the applet
		p = new Playfield(this, bounds().width, bounds().height); 

		// Create a simple sprite
		Sprite s = new Sprite(p);

		// Tell the sprite to bounce at the edges of the playfield
		s.setEdgeHandling(Sprite.edgeBounce);

		// Fetch an image
		Image image = p.getImage(base + "wanderer.gif");

		// Set this as the image for the sprite
		s.setImage(image);

		// Set a direction (45 degrees)
		s.setDirection(45);

		// Start it moving: 30 pixels per second
		s.setSpeed(30);

		// Now start movement in the playfield
		p.start();
	
		// Now display the playfield
		add(p);
	}	
	// When the user leaves the web page
	public void stop() {
		p.suspend();
	}
	// When the user comes back
	public void start() {
		p.resume();
	}
	// When the browser wants the applet to go away
	public void destroy() {
		p.stop();
	}
};

baklava.Playfield Method Reference

This section provides a reference guide to the publicly accessible methods (functions) available in the baklava.Playfield class.
Playfield(Applet appletArg, int wArg, int hArg)
This method is the constructor of the class, used to create a new playfield. Note that the applet must be the first argument to this method (see the example below). The width and height must also be set at construction time. For example:
import baklava.*;

class MyApplet extends Applet {
	public void init() {
		setLayout(null);
		Playfield p = 
			new Playfield(this, 
				bounds().width, bounds().height);

		// Create various sprites...

		// Now start movement in the playfield
		p.start();
	}
};
void setGlobalTimerObserver(GlobalTimerObserver observerArg)
This method is used by applets that wish to receive global timer events. The applet must implement the baklava.GlobalTimerObserver interface, call this method, implement a public void globalTimer(int timerId) method at which to receive the events, and then call setGlobalTimer as desired to set timers. Example:
import baklava.*;

// Be sure to specify that we implement the GlobalTimerObserver interface
class MyApplet extends Applet implements GlobalTimerObserver {
	static final int helloTimer = 1;
	Playfield p;
	public void init() {
		setLayout(null);
		p = new Playfield(this, 
				bounds().width, bounds().height);
		// This is valid because we implement GlobalTimerObserver
		p.setGlobalTimerObserver(this);
		// Send a helloTimer event in 1 second (1000 milliseconds)
		setGlobalTimer(1000, helloTimer);

		// Create various sprites...

		// Now start movement in the playfield
		p.start();
	}
	public void globalTimer(int timerId)
	{
		// This will print 1 in the java console.
		system.out.println(timerId);
		// Now set the timer again.
		setGlobalTimer(1000, helloTimer);
	}
};
void setGlobalTimer(int delay, int timerId)
This method is used to set a global timer. A global timer event will be delivered after delay milliseconds (thousands of a second). The timerId argument will be passed to the globalTimer method of the global timer observer. For more information, and an example, see the discussion of the setGlobalTimerObserver method. See also Sprite.setTimer for a way to send a timer event to an individual sprite.
void suspend()
This method is used to temporarily suspend all activity in the playfield until the resume method is called. The playfield will continue to repaint itself as needed. Technical note: To avoid problems with some implementations of java, the Thread.suspend() function is not used in the implementation. Instead, baklava spends most of its time "sleeping" to conserve CPU, and does not simulate motion while suspended. You may safely call Playfield.suspend() in all implementations of java.
void resume()
This method is used to resume activity in the playfield after the method has been called.
void start()
This method is used to start activity in the playfield. Until this method is called, sprites do not appear in the playfield and their movement is not calculated. It is very important to call this method after initializing the playfield and the sprites. For a way to suspend and restart the playfield's activity later, see the suspend and resume methods.
Image getImage(String url) OR Image getImage(URL url)
The getImage method accesses the indicated URL and downloads an image from that location. This image can then be used as an argument to the Sprite.setImage method or the Sprite.setTile method. Images can be in any format supported by the Java implementation, typically GIF or JPEG. If the start method has not yet been called for the playfield, Baklava will display a progress indicator in the playfield until all of the requested images have been downloaded successfully. You can change this behavior using the setProgressMessage and setShowProgress methods. For best results, first call getImage for all of the images you will need. Then call Sprite.setImage to take advantage of each of them. Note that transparent GIFs are fully supported by Java, and transparent pixels are ignored by Baklava when determining collisions between sprites. This is allows non-square objects to behave in realistic ways.
void progressStart()
Baklava features a progress indicator display which is automatically used to indicate that images are being downloaded. In addition, you can use this progress indicator for other purposes by calling progressStart to indicate the beginning of a lengthy operation and progressEnd to indicate the end of the operation. Calls to these methods can be freely nested, as long as there is a progressEnd call for each corresponding progressStart call. See also setProgressMessage.
void progressEnd()
The progressEnd method is used to shut off the progress indicator. See progressStart for more information.
void imagesNeededNow
In certain